import javax.swing.*;
        import java.awt.*;
        import java.awt.event.*;
        import java.util.*;

public class Main extends JPanel implements ActionListener {  // 将类名改为 Main

    private final int TILE_SIZE = 25;
    private final int WIDTH = 600;
    private final int HEIGHT = 600;
    private final int NUM_TILES_X = WIDTH / TILE_SIZE;
    private final int NUM_TILES_Y = HEIGHT / TILE_SIZE;

    private boolean running = false;
    private int score = 0;
    private LinkedList<Point> snake;
    private Point food;
    private char direction = 'R'; // R = Right, L = Left, U = Up, D = Down
    private javax.swing.Timer timer;  // 明确使用 javax.swing.Timer

    public Main() {
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setBackground(Color.BLACK);
        setFocusable(true);
        addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                switch (e.getKeyCode()) {
                    case KeyEvent.VK_UP:
                        if (direction != 'D') direction = 'U';
                        break;
                    case KeyEvent.VK_DOWN:
                        if (direction != 'U') direction = 'D';
                        break;
                    case KeyEvent.VK_LEFT:
                        if (direction != 'R') direction = 'L';
                        break;
                    case KeyEvent.VK_RIGHT:
                        if (direction != 'L') direction = 'R';
                        break;
                }
            }
        });
        initializeGame();
    }

    private void initializeGame() {
        snake = new LinkedList<>();
        snake.add(new Point(NUM_TILES_X / 2, NUM_TILES_Y / 2)); // 初始位置
        spawnFood();
        running = true;
        timer = new javax.swing.Timer(100, this);  // 明确使用 javax.swing.Timer
        timer.start();
    }

    private void spawnFood() {
        Random rand = new Random();
        int x = rand.nextInt(NUM_TILES_X);
        int y = rand.nextInt(NUM_TILES_Y);
        food = new Point(x, y);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (running) {
            moveSnake();
            checkCollision();
            checkFoodCollision();
            repaint();
        }
    }

    private void moveSnake() {
        Point head = snake.getFirst();
        Point newHead = null;

        switch (direction) {
            case 'R': newHead = new Point(head.x + 1, head.y); break;
            case 'L': newHead = new Point(head.x - 1, head.y); break;
            case 'U': newHead = new Point(head.x, head.y - 1); break;
            case 'D': newHead = new Point(head.x, head.y + 1); break;
        }

        // 使蛇尾跟随头部的移动
        snake.addFirst(newHead);
        snake.removeLast();
    }

    private void checkCollision() {
        Point head = snake.getFirst();

        // 撞墙
        if (head.x < 0 || head.x >= NUM_TILES_X || head.y < 0 || head.y >= NUM_TILES_Y) {
            gameOver();
        }

        // 撞自己
        for (int i = 1; i < snake.size(); i++) {
            if (head.equals(snake.get(i))) {
                gameOver();
                break;
            }
        }
    }

    private void checkFoodCollision() {
        Point head = snake.getFirst();
        if (head.equals(food)) {
            // 食物被吃掉，蛇变长
            score++;
            snake.addLast(snake.getLast());
            spawnFood();
        }
    }

    private void gameOver() {
        running = false;
        timer.stop();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        drawGame(g);
    }

    private void drawGame(Graphics g) {
        if (running) {
            // 绘制蛇
            g.setColor(Color.GREEN);
            for (Point p : snake) {
                g.fillRect(p.x * TILE_SIZE, p.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
            }

            // 绘制食物
            g.setColor(Color.RED);
            g.fillRect(food.x * TILE_SIZE, food.y * TILE_SIZE, TILE_SIZE, TILE_SIZE);

            // 绘制得分
            g.setColor(Color.WHITE);
            g.setFont(new Font("Arial", Font.PLAIN, 20));
            g.drawString("Score: " + score, 10, 20);
        } else {
            // 游戏结束画面
            g.setColor(Color.WHITE);
            g.setFont(new Font("Arial", Font.BOLD, 30));
            g.drawString("Game Over", WIDTH / 2 - 100, HEIGHT / 2);
            g.setFont(new Font("Arial", Font.PLAIN, 20));
            g.drawString("Score: " + score, WIDTH / 2 - 50, HEIGHT / 2 + 30);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("贪吃蛇");
        Main game = new Main();  // 创建 Main 类的实例
        frame.add(game);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}